package com.study.demo_resttemplate.utils;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.Base64;

/**
 * @Name: FileUtil
 * @Description: java类作用描述
 * @Author: yn
 * @CreateDate: 2022/7/15 15:39
 * @Remark: 特殊说明
 * @Version: 1.0
 */
public class FileUtil {
    public static String encryptToBase64(String filePath) {
        if (filePath == null) {
            return null;
        }
        try {
            byte[] b = Files.readAllBytes(Paths.get(filePath));
            return Base64.getEncoder().encodeToString(b);
        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;
    }
    /**
     * 输入转化后的base64和要生成的问文件位置
     * @param base64
     * @param filePath
     * @return
     */
    public static  String decryptByBase64(String base64, String filePath) {
        if (base64 == null && filePath == null) {
            return "生成文件失败，请给出相应的数据。";
        }
        try {
            Files.write(Paths.get(filePath), Base64.getDecoder().decode(base64), StandardOpenOption.CREATE);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "指定路径下生成文件成功！";
    }
}
